package com.ict.ais.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ict.ais.abnormal.facade.api.IAbnormalService;
import com.ict.ais.abnormal.facade.common.SplitUtils;
import com.ict.ais.abnormal.facade.dto.AreaSet;
import com.ict.ais.abnormal.facade.dto.ResultObj;
import com.ict.ais.abnormal.facade.model.AISOffLine;
import com.ict.ais.abnormal.facade.model.AbnormalSet;
import com.ict.ais.abnormal.facade.model.AnomalyGrids;
import com.ict.ais.abnormal.facade.model.AnomalyShips;
import com.ict.ais.abnormal.facade.model.ZoneEntry;
import com.ict.ais.abnormal.facade.model.ZoneEntryCache;
import com.ict.ais.area.facade.api.IAreaAddService;
import com.ict.ais.area.facade.api.IAreaOpeService;
import com.ict.ais.area.facade.model.Point;
import com.ict.ais.common.StateMsg;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by chenlong on 2016/12/8.
 */

/**
 * 异常告警
 */
@Controller
@RequestMapping("/abnormal")
public class AbnormalController {
    @Resource
    IAbnormalService abnormalService;

    @Resource
    IAreaAddService service;
    @Resource
    IAreaOpeService areaOpeService;

    /**
     * 区域异常设置添加-矩形
     * @param map
     * @param ldlon 左下经度
     * @param ldlat 左下纬度
     * @param rulon 右上经度
     * @param rulat 右上纬度
     * @param areaName 区域名
     * @param user 用户名
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "areaset.do",method = RequestMethod.POST)
    public String areaSetAdd(ModelMap map, @RequestParam(value = "ldlon",required = false) double ldlon,
                               @RequestParam(value = "ldlat",required = false) double ldlat,
                               @RequestParam(value = "rulon",required = false) double rulon,
                               @RequestParam(value = "rulat",required = false) double rulat,
                               @RequestParam(value = "areaName",required = false) String areaName,
                               @RequestParam(value = "user",required = false) String user){
        String msg;
        String jsonStr;

        Point p1=new Point(ldlon,ldlat);
        Point p2=new Point(rulon,rulat);

        int areaId=service.addRectangle(p1,p2,areaName,user,0);

        if(areaId==-1){
            msg="区域添加失败";
            map.addAttribute("result",msg);
            jsonStr= StateMsg.toJson(-1,map);
            return jsonStr;
        }else{
            AreaSet areaSet=new AreaSet(ldlon,ldlat,rulon,rulat,areaName,areaId);
            boolean ifSuccess=abnormalService.areaAbnormalSetRectangle(areaSet);
            if(ifSuccess){
                msg="异常设置命令发送成功";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(1,map);
                return jsonStr;
            }else{
                msg="异常设置命令发送失败";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(-2,map);
                return jsonStr;
            }
        }
    }

    /**
     * 区域异常设置添加-多边形
     * @param map
     * @param lonStrArr
     * @param latStrArr
     * @param areaName
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "areasetpolygons.do",method = RequestMethod.POST)
    public String areaSetAddPolygons(ModelMap map, @RequestParam(value = "lonStrArr",required = false) String lonStrArr,
                             @RequestParam(value = "latStrArr",required = false) String latStrArr,
                             @RequestParam(value = "areaName",required = false) String areaName,
                             @RequestParam(value = "user",required = false) String user){
        String msg;
        String jsonStr="";
        try {
            String[] lonArr = SplitUtils.MDVNumSplit_StrArray(lonStrArr);
            String[] latArr = SplitUtils.MDVNumSplit_StrArray(latStrArr);
            if(lonArr.length!=latArr.length){
                msg="异常设置命令发送失败";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(-2,map);
                return jsonStr;
            }else {
                Point[] points=new Point[lonArr.length];
                for (int i=0;i<lonArr.length;i++){
                    Point p1=new Point(Double.parseDouble(lonArr[i]),Double.parseDouble(latArr[i]));
                    points[i]=p1;
                }

                int areaId=service.addPolygon(points,areaName,user,0);
                if(areaId==-1){
                    msg="区域添加失败";
                    map.addAttribute("result",msg);
                    jsonStr= StateMsg.toJson(-1,map);
                    return jsonStr;
                }else{
                    boolean ifSuccess=abnormalService.areaAbnormalSetPolygons(points,areaName,areaId);
                    if(ifSuccess){
                        msg="异常设置命令发送成功";
                        map.addAttribute("result",msg);
                        jsonStr=StateMsg.toJson(1,map);
                        return jsonStr;
                    }else{
                        msg="异常设置命令发送失败";
                        map.addAttribute("result",msg);
                        jsonStr=StateMsg.toJson(-2,map);
                        return jsonStr;
                    }
                }
            }
        }catch (Exception e){
            msg="异常设置命令发送失败";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(-2,map);
            return jsonStr;
        }
    }

    /**
     * 区域异常设置添加-圆形
     * @param map
     * @param cirlon 圆心经度
     * @param cirlat 圆心维度
     * @param radius 半径，单位：米
     * @param areaName 区域名
     * @param user 用户名
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "areasetcircle.do",method = RequestMethod.POST)
    public String areasetCircle(ModelMap map, @RequestParam(value = "cirlon",required = false) double cirlon,
                             @RequestParam(value = "cirlat",required = false) double cirlat,
                             @RequestParam(value = "radius",required = false) int radius,
                             @RequestParam(value = "areaName",required = false) String areaName,
                             @RequestParam(value = "user",required = false) String user){
        String msg;
        String jsonStr="";
        Point p1=new Point(cirlon,cirlat);

        int areaId=service.addCircle(p1,radius,areaName,user,0);

        if(areaId==-1){
            msg="区域添加失败";
            map.addAttribute("result",msg);
            jsonStr= StateMsg.toJson(-1,map);
            return jsonStr;
        }else{
            boolean ifSuccess=abnormalService.areaAbnormalSetCircle(p1,radius,areaName,areaId);
            if(ifSuccess){
                msg="异常设置命令发送成功";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(1,map);
                return jsonStr;
            }else{
                msg="异常设置命令发送失败";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(-2,map);
                return jsonStr;
            }
        }
    }

    /**
     * 区域异常设置添加- 扇（环）形
     * @param map
     * @param lon
     * @param lat
     * @param radius
     * @param radsmal
     * @param start_deg
     * @param end_deg
     * @param areaName
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "areasetsector.do",method = RequestMethod.POST)
    public String areasetSector(ModelMap map, @RequestParam(value = "lon",required = false) double lon,
                             @RequestParam(value = "lat",required = false) double lat,
                             @RequestParam(value = "radius",required = false) int radius,
                             @RequestParam(value = "radsmal",required = false) int radsmal,
                             @RequestParam(value = "start_deg",required = false) double start_deg,
                             @RequestParam(value = "end_deg",required = false) double end_deg,
                             @RequestParam(value = "areaName",required = false) String areaName,
                             @RequestParam(value = "end_deg",required = false) String user){
        String msg;
        String jsonStr="";
        Point p1=new Point(lon,lat);

        int areaId=service.addSector(p1,radius,radsmal,start_deg,end_deg,areaName,user,0);

        if(areaId==-1){
            msg="扇形区域添加失败";
            map.addAttribute("result",msg);
            jsonStr= StateMsg.toJson(-1,map);
            return jsonStr;
        }else{
            boolean ifSuccess=abnormalService.areaAbnormalSetSector(p1,radius,radsmal,start_deg,end_deg,areaName,areaId);
            if(ifSuccess){
                msg="异常设置命令发送成功";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(1,map);
                return jsonStr;
            }else{
                msg="异常设置命令发送失败";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(-2,map);
                return jsonStr;
            }
        }
    }

    /**
     * 目标列表
     * @param map
     * @param areaIdArr
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "areasetdelete.do",method = RequestMethod.POST)
    public String areaSetDelete(ModelMap map, @RequestParam(value = "areaIdArr",required = false) String areaIdArr){
        String msg;
        String jsonStr;
        
        
        
        

        boolean ifSuccess=abnormalService.areaSetDelete(areaIdArr);
        
        if(ifSuccess){
        	try {
				String[] StrArray = SplitUtils.MDVNumSplit_StrArray(areaIdArr);
				for(String  strAreaIds: StrArray) {
					int  intAreaId=Integer.parseInt(strAreaIds);
					areaOpeService.delArea(intAreaId, 255);
				}
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            msg="异常设置命令发送成功";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(1,map);
            return jsonStr;
        }else{
            msg="异常设置命令发送失败";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(-1,map);
            return jsonStr;
        }
    }

    /**
     *
     * @param map
     * @param closetime 设置ais关闭的时间，单位：秒
     * @param tp1 货船速度设置，单位节
     * @param tp2 搜救船速度设置，单位节
     * @param tp3 油轮速度设置，单位节
     * @param tp4 拖船速度设置，单位节
     * @param tp5 渔船速度设置，单位节
     * @param tp6 拖船速度设置，单位节
     * @param tp7 客船速度设置，单位节
     * @param tp8 军事行动船速度设置，单位节
     * @param tp9 渔政执法船速度设置，单位节
     * @param tp11 风效应船速度设置，单位节
     * @param defa 其他船速度设置，单位节
     * @param count 设置区域目标数量
     * @return
     */
    //fcc修改部分
    @ResponseBody
    @RequestMapping(value = "abnormal_aisset.do",method = RequestMethod.POST)
    public String abnormal_aisset(ModelMap map, @RequestParam(value = "closetime",required = false) int closetime
            

    ){
        String msg;
        String jsonStr;

        AbnormalSet abnormalSet=new AbnormalSet();
        abnormalSet.setClosetime(closetime);

        boolean ifSuccess=abnormalService.otherAbnormalSet(abnormalSet);
        if(ifSuccess){
            boolean ifSuc=abnormalService.updateAbnormalSet(abnormalSet);
            if(ifSuc){
                msg="异常设置命令发送成功";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(1,map);
                return jsonStr;
            }else{
                msg="异常设置命令发送成功,默认设置更新失败";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(-2,map);
                return jsonStr;
            }
        }else{
            msg="异常设置命令发送失败";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(-1,map);
            return jsonStr;
        }
    }
    
    @ResponseBody
    @RequestMapping(value = "abnormalset.do",method = RequestMethod.POST)
    public String areaSetAbnormal(ModelMap map, @RequestParam(value = "tp1",required = false) int tp1
            , @RequestParam(value = "tp2",required = false) int tp2
            , @RequestParam(value = "tp3",required = false) int tp3
            , @RequestParam(value = "tp4",required = false) int tp4
            , @RequestParam(value = "tp5",required = false) int tp5
            , @RequestParam(value = "tp6",required = false) int tp6
           
          
            , @RequestParam(value = "defa",required = false) int defa//其他
            

    ){
        String msg;
        String jsonStr;

        AbnormalSet abnormalSet=new AbnormalSet(tp1,tp2,tp3,tp4,tp5,tp6,defa);

        boolean ifSuccess=abnormalService.otherAbnormalSet(abnormalSet);
        if(ifSuccess){
            boolean ifSuc=abnormalService.updateAbnormalSet(abnormalSet);
            if(ifSuc){
                msg="异常设置命令发送成功";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(1,map);
                return jsonStr;
            }else{
                msg="异常设置命令发送成功,默认设置更新失败";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(-2,map);
                return jsonStr;
            }
        }else{
            msg="异常设置命令发送失败";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(-1,map);
            return jsonStr;
        }
    }
    @ResponseBody
    @RequestMapping(value = "abnormal_target_set.do",method = RequestMethod.POST)
    public String abnormal_target_set(ModelMap map, 
             @RequestParam(value = "count",required = false) int count

    ){
        String msg;
        String jsonStr;

        AbnormalSet abnormalSet=new AbnormalSet();
        abnormalSet.setCou(count);
        boolean ifSuccess=abnormalService.otherAbnormalSet(abnormalSet);
        if(ifSuccess){
            boolean ifSuc=abnormalService.updateAbnormalSet(abnormalSet);
            if(ifSuc){
                msg="异常设置命令发送成功";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(1,map);
                return jsonStr;
            }else{
                msg="异常设置命令发送成功,默认设置更新失败";
                map.addAttribute("result",msg);
                jsonStr=StateMsg.toJson(-2,map);
                return jsonStr;
            }
        }else{
            msg="异常设置命令发送失败";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(-1,map);
            return jsonStr;
        }
    }
    
    /**
     * 异常设置获取
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getabset.do",method = RequestMethod.GET)
    public String getAbnormalSet(ModelMap map){
        String msg;
        String jsonStr;

        AbnormalSet abnormalSet=abnormalService.getAbnormalSet();
        if(abnormalSet==null){
            msg="获取异常设置失败";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(-1,map);
            return jsonStr;
        }

        jsonStr=StateMsg.toJsonObj(1,abnormalSet);
        return jsonStr;
    }

    /**
     * 获取驶入敏感地带异常
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getzoneentry.do",method = RequestMethod.POST)
    public String getZoneEntry(ModelMap map, @RequestParam(value = "tim",required = false) int tim){
        return getZoneEntry(map, tim, 0);
    }

    /**
     * 获取驶入敏感地带异常 -pro 带limit
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getzoneentrypro.do",method = RequestMethod.POST)
    public String getZoneEntry(ModelMap map,@RequestParam(value = "tim",required = false) int tim,@RequestParam(value = "limit",required = false) int limit){
        String msg;
        String jsonStr;

        List<ZoneEntry> zoneEntryList=abnormalService.getZoneEntry(tim,limit);
        if(zoneEntryList.size()==0){
            msg="获取驶入敏感地带异常信息失败";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(-1,map);
            return jsonStr;
        }

        map.addAttribute("anomalyList",zoneEntryList);
        jsonStr=StateMsg.toJson(1,map);
        return jsonStr;
    }

    /**
     * 获取船速过快异常
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getanomalyships.do",method = RequestMethod.POST)
    public String getAnomalyShips(ModelMap map, @RequestParam(value = "tim",required = false) int tim){
        return getAnomalyShips(map, tim, 0);
    }

    /**
     * 获取船速过快异常 -pro 带limit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getanomalyshipspro.do",method = RequestMethod.POST)
    public String getAnomalyShips(ModelMap map, @RequestParam(value = "tim", required = false) int tim, @RequestParam(value = "limit", required = false) int limit){
        String msg;
        String jsonStr;

        List<AnomalyShips> anomalyShipsList=abnormalService.getAnomalyShips(tim,limit);
        if(anomalyShipsList.size()==0){
            msg ="获取船速过快异常信息失败";
            map.addAttribute("result", msg);
            jsonStr=StateMsg.toJson(-1,map);
            return jsonStr;
        }

        map.addAttribute("anomalyList",anomalyShipsList);
        jsonStr=StateMsg.toJson(1,map);
        return jsonStr;
    }

    /**
     * 疑似JS目标异常获取
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getanomalyjs.do",method = RequestMethod.POST)
    public String getAnomalyJS(ModelMap map,
                               @RequestParam(value = "tim",required = false) int tim,
                               @RequestParam(value = "limit",required = false) int limit){
        String msg;
        String jsonStr;

        List<AnomalyShips> anomalyShipsList=abnormalService.getAnomalyJS(tim,limit);
        if(anomalyShipsList.size()==0){
            msg="获取异常信息失败";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(-1,map);
            return jsonStr;
        }

        map.addAttribute("anomalyList",anomalyShipsList);
        jsonStr=StateMsg.toJson(1,map);
        return jsonStr;
    }

    /**
     * 疑似执法目标异常获取
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getanomalyzf.do",method = RequestMethod.POST)
    public String getAnomalyZF(ModelMap map, @RequestParam(value = "tim",required = false) int tim,@RequestParam(value = "limit",required = false) int limit){
        String msg;
        String jsonStr;

        List<AnomalyShips> anomalyShipsList=abnormalService.getAnomalyZF(tim,limit);
        if(anomalyShipsList.size()==0){
            msg="获取异常信息失败";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(-1,map);
            return jsonStr;
        }

        map.addAttribute("anomalyList",anomalyShipsList);
        jsonStr=StateMsg.toJson(1,map);
        return jsonStr;
    }

    /**
     * 获取异常聚集异常
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getanomalygrids.do",method = RequestMethod.POST)
    public String getAnomalyGrids(ModelMap map, @RequestParam(value = "tim",required = false) int tim) {
        return getAnomalyGrids(map,tim,0);
    }

    /**
    * 获取异常聚集异常 -pro 带limit
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "getanomalygridspro.do",method = RequestMethod.POST)
    public String getAnomalyGrids(ModelMap map, @RequestParam(value = "tim",required = false) int tim,@RequestParam(value = "limit",required = false) int limit){
        String msg;
        String jsonStr;

        List<AnomalyGrids> anomalyGridsList=abnormalService.getAnomalyGrids(tim,limit);
        if(anomalyGridsList.size()==0){

            msg="获取异常聚集异常信息失败";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(-1,map);
            return jsonStr;
        }

        map.addAttribute("anomalyList",anomalyGridsList);
        jsonStr=StateMsg.toJson(1,map);
        return jsonStr;
    }

    /**
     * 获取AIS关闭异常
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getaisoffline.do",method = RequestMethod.POST)
    public String getAISOffLine(ModelMap map, @RequestParam(value = "tim",required = false) int tim) {
        return getAISOffLine(map,tim,0);
    }

    /**
    * 获取AIS关闭异常 -pro 带limit
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "getaisofflinepro.do",method = RequestMethod.POST)
    public String getAISOffLine(ModelMap map,
                                @RequestParam(value = "tim",required = false) int tim,
                                @RequestParam(value = "limit",required = false) int limit){
        String msg;
        String jsonStr;

        List<AISOffLine> aisOffLineList=abnormalService.getAISOffLine(tim,limit);
        if(aisOffLineList.size()==0){
            msg="获取AIS关闭异常信息失败";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(-1,map);
            return jsonStr;
        }

        map.addAttribute("anomalyList",aisOffLineList);
        jsonStr=StateMsg.toJson(1,map);
        return jsonStr;
    }

    /**
     * 重点目标异常告警
     * @param map
     * @param tim
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getimtargetexc.do",method = RequestMethod.POST)
    public String getimtargetexc(ModelMap map,
                                @RequestParam(value = "tim",required = false) int tim,
                                @RequestParam(value = "limit",required = false) int limit){
        String msg;
        String jsonStr;

        ResultObj resultObj =abnormalService.getImTarget(tim,limit);
        if(resultObj.getState()==0){
            msg="列表为空";
            map.addAttribute("error",msg);
            jsonStr = StateMsg.toJson(0, map);
            return jsonStr;
        }else if(resultObj.getState()==-1){
            msg="获取列表失败";
            map.addAttribute("error",msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }else {
            map.addAttribute("shipList",resultObj.getObj());
            jsonStr = StateMsg.toJson(1, map);
            return jsonStr;
        }
    }

    /**
     * 驶入敏感区域缓冲区-限制个数
     * @param map
     * @param tim
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getzoneentrycache.do",method = RequestMethod.POST)
    public String getzoneentrycache(ModelMap map,
                                 @RequestParam(value = "tim",required = false) int tim,
                                 @RequestParam(value = "limit",required = false) int limit){
        String msg;
        String jsonStr;

        List<ZoneEntryCache> zoneEntryList=abnormalService.getZoneEntryCache(tim,limit);
        if(zoneEntryList.size()==0){
            msg="获取驶入敏感缓冲地带异常信息失败";
            map.addAttribute("result",msg);
            jsonStr=StateMsg.toJson(-1,map);
            return jsonStr;
        }

        map.addAttribute("anomalyList",zoneEntryList);
        jsonStr=StateMsg.toJson(1,map);
        return jsonStr;
    }
}